home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / share / initramfs-tools / scripts / casper-helpers < prev    next >
Text File  |  2008-10-25  |  6KB  |  233 lines

  1. ## Casper helper functions, used by casper on boot and by casper-snapshot
  2.  
  3. if [ "${BUILD_SYSTEM}" = "Ubuntu" ]; then
  4.     MP_QUIET="-Q"
  5. elif [ "${BUILD_SYSTEM}" = "Debian" ]; then
  6.     MP_QUIET="-q"
  7. else
  8.     MP_QUIET=""
  9. fi
  10.  
  11. if [ ! -x "/bin/fstype" ]; then
  12.     # klibc not in path -> not in initramfs
  13.     export PATH="${PATH}:/usr/lib/klibc/bin"
  14. fi
  15.  
  16. sys2dev() {
  17.     sysdev=${1#/sys}
  18.     echo "/dev/$(/sbin/udevadm info -q name -p ${sysdev} 2>/dev/null|| echo ${sysdev##*/})"
  19. }
  20.  
  21. subdevices() {
  22.     sysblock=$1
  23.     r=""
  24.     for dev in "${sysblock}" "${sysblock}"/*; do
  25.         if [ -e "${dev}/dev" ]; then
  26.             r="${r} ${dev}"
  27.         fi
  28.     done
  29.     echo ${r}
  30. }
  31.  
  32. is_supported_fs () {
  33.     # FIXME: do something better like the scan of supported filesystems
  34.     fstype="${1}"
  35.     case ${fstype} in
  36.         vfat|iso9660|udf|ext2|ext3|ntfs)
  37.             return 0
  38.             ;;
  39.     esac
  40.     return 1
  41. }
  42.  
  43. get_fstype() {
  44.     local FSTYPE
  45.     local FSSIZE
  46.     eval $(fstype < $1)
  47.     if [ "$FSTYPE" != "unknown" ]; then
  48.         echo $FSTYPE
  49.         return 0
  50.     fi
  51.     /lib/udev/vol_id -t $1 2>/dev/null
  52. }
  53.  
  54. where_is_mounted() {
  55.     device=$1
  56.     if grep -q "^$device " /proc/mounts; then
  57.         grep "^$device " /proc/mounts | read d mountpoint rest
  58.         echo $mountpoint
  59.         return 0
  60.     fi
  61.     return 1
  62. }
  63.  
  64. lastline() {
  65.     while read lines ; do
  66.         line=${lines}
  67.     done
  68.     echo "${line}"
  69. }
  70.  
  71. base_path ()
  72. {
  73.     testpath="${1}"
  74.     mounts="$(awk '{print $2}' /proc/mounts)"
  75.     testpath="$(busybox realpath ${testpath})"
  76.  
  77.     while true ; do
  78.         if echo "${mounts}" | grep -qs "^${testpath}" ; then
  79.             set -- `echo "${mounts}" | grep "^${testpath}" | lastline`
  80.             echo ${1}
  81.             break
  82.         else
  83.             testpath=`dirname $testpath`
  84.         fi
  85.     done
  86. }
  87.  
  88. fs_size ()
  89. {
  90.     # Returns used/free fs kbytes + 5% more
  91.     # You could pass a block device as $1 or the mount point as $2
  92.  
  93.     dev="${1}"
  94.     mountp="${2}"
  95.     used="${3}"
  96.  
  97.     if [ -z "${mountp}" ]; then
  98.         mountp=$(where_is_mounted "${dev}")
  99.         if [ "$?" -gt 0 ]; then
  100.             mountp="/mnt/tmp_fs_size"
  101.             mkdir -p "${mountp}"
  102.             mount -t $(get_fstype "${dev}") -o ro "${dev}" "${mountp}"
  103.             doumount=1
  104.         fi
  105.     fi
  106.  
  107.     if [ "${used}" = "used" ]; then
  108.         size=$(du -ks ${mountp} | cut -f1)
  109.         size=$(expr ${size} + ${size} / 20 ) # FIXME: 5% more to be sure
  110.     else
  111.         # free space
  112.         size="$(df -k | grep -s ${mountp} | awk '{print $4}')"
  113.     fi
  114.  
  115.     if [ -n "${doumount}" ]; then
  116.         umount "${mountp}"
  117.         rmdir "${mountp}"
  118.     fi
  119.     echo "${size}"
  120. }
  121.  
  122. setup_loop() {
  123.     local fspath=$1
  124.     local module=$2
  125.     local pattern=$3
  126.     local offset=$4
  127.  
  128.     modprobe ${MP_QUIET} -b "$module"
  129.     /sbin/udevadm settle
  130.  
  131.     if [ "$module" = loop ]; then
  132.         if [ ! -e /dev/loop0 ]; then
  133.             # temporary workaround for kernel bug
  134.             for i in 0 1 2 3 4 5 6 7; do
  135.                 mknod "/dev/loop$i" b 7 "$i" || true
  136.             done
  137.         fi
  138.  
  139.         dev="$(losetup -f)"
  140.         if [ "$dev" ]; then
  141.             if [ -n "$offset" ]; then
  142.                 losetup -o "$offset" "$dev" "$fspath"
  143.             else
  144.                 losetup "$dev" "$fspath"
  145.             fi
  146.             echo "$dev"
  147.             return 0
  148.         else
  149.             panic "No loop devices available"
  150.         fi
  151.     else
  152.         for loopdev in $pattern; do
  153.             if [ "$(cat $loopdev/size)" -eq 0 ]; then
  154.                 dev=$(sys2dev "${loopdev}")
  155.                 if [ -n "$offset" ]; then
  156.                     losetup -o "$offset" "$dev" "$fspath"
  157.                 else
  158.                     losetup "$dev" "$fspath"
  159.                 fi
  160.                 echo "$dev"
  161.                 return 0
  162.             fi
  163.         done
  164.         panic "No loop devices available"
  165.     fi
  166. }
  167.  
  168. try_mount ()
  169. {
  170.     dev="${1}"
  171.     mountp="${2}"
  172.     opts="${3}"
  173.  
  174.     if where_is_mounted ${dev} > /dev/null; then
  175.     if [ "${opts}" != "ro" ]; then
  176.         mount -o remount,"${opts}" ${dev} $(where_is_mounted ${dev}) || panic "Remounting failed"
  177.     fi
  178.         mount -o bind $(where_is_mounted ${dev}) ${mountp} || panic "Cannot bind-mount"
  179.     else
  180.         mount -t $(get_fstype "${dev}") -o "${opts}" "${dev}" "${mountp}" || panic "Cannot mount ${dev} on ${mountp}"
  181.     fi
  182. }
  183.  
  184. find_cow_device() {
  185.     pers_label="${1}"
  186.     cow_backing="/${pers_label}-backing"
  187.     for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop); do
  188.         for dev in $(subdevices "${sysblock}"); do
  189.             devname=$(sys2dev "${dev}")
  190.             if [ "$(/lib/udev/vol_id -l $devname 2>/dev/null)" = "${pers_label}" ]; then
  191.                 echo "$devname"
  192.                 return
  193.             elif [ "$(get_fstype ${devname})" = "vfat" ]; then # FIXME: all supported block devices should be scanned
  194.                 mkdir -p "${cow_backing}"
  195.                 try_mount "${devname}" "${cow_backing}" "rw"
  196.                 if [ -e "${cow_backing}/${pers_label}" ]; then
  197.                     echo $(setup_loop "${cow_backing}/${pers_label}" "loop" "/sys/block/loop*")
  198.                     return 0
  199.                 else
  200.                     umount ${cow_backing}
  201.                 fi
  202.             fi
  203.         done
  204.     done
  205. }
  206.  
  207. find_files()
  208. # return the first of $filenames found on vfat and ext2 devices
  209. # FIXME: merge with above function
  210. {
  211.     filenames="${1}"
  212.     snap_backing="/snap-backing"
  213.     for sysblock in $(echo /sys/block/* | tr ' ' '\n' | grep -v loop); do
  214.         for dev in $(subdevices "${sysblock}"); do
  215.             devname=$(sys2dev "${dev}")
  216.             devfstype="$(get_fstype ${devname})"
  217.             if [ "${devfstype}" = "vfat" ] ||  [ "${devfstype}" = "ext2" ] ; then # FIXME: all supported block devices should be scanned
  218.                 mkdir -p "${snap_backing}"
  219.                 try_mount "${devname}" "${snap_backing}" "ro"
  220.                 for filename in ${filenames}; do
  221.                     if [ -e "${snap_backing}/${filename}" ]; then
  222.                         echo "${devname} ${snap_backing} ${filename}"
  223.                         return 0
  224.                     fi
  225.                 done
  226.                 umount ${snap_backing}
  227.             fi
  228.         done
  229.     done
  230. }
  231.  
  232.  
  233.